package pt.mleiria.sAction.web.controller;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.dao.DataAccessException;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import pt.mleiria.sAction.bo.chart.BuildXYChartBO;
import pt.mleiria.sAction.bo.combos.LoadCombosBO;
import pt.mleiria.sAction.bo.index.IndexBO;
import pt.mleiria.sAction.bo.mainPage.MainPageCommand;
import pt.mleiria.sAction.bo.stock.StockBO;
import pt.mleiria.sAction.cfg.MenuDigesterDriver;
import pt.mleiria.sAction.cfg.Menu_Items;
import pt.mleiria.sAction.datasetProducer.XYChartProducer;
import pt.mleiria.sAction.hibernate.IndexData;
import pt.mleiria.sAction.hibernate.Stock;
import pt.mleiria.sAction.utils.Constants;
import pt.mleiria.sAction.vo.UtilizadorVO;
import pt.mleiria.sAction.vo.chart.ChartVO;

public class MainPageController extends SimpleFormController {
	private IndexBO indexBO;
	private StockBO stockBO;
	private BuildXYChartBO buildXYChartBO;
	private LoadCombosBO loadCombosBO;
	
	/**
	 * @return the loadCombosBO
	 */
	public LoadCombosBO getLoadCombosBO() {
		return loadCombosBO;
	}

	/**
	 * @param loadCombosBO the loadCombosBO to set
	 */
	public void setLoadCombosBO(LoadCombosBO loadCombosBO) {
		this.loadCombosBO = loadCombosBO;
	}

	/**
	 * @return the stockBO
	 */
	public StockBO getStockBO() {
		return stockBO;
	}

	/**
	 * @param stockBO the stockBO to set
	 */
	public void setStockBO(StockBO stockBO) {
		this.stockBO = stockBO;
	}

	/**
	 * @return the buildXYChartBO
	 */
	public BuildXYChartBO getBuildXYChartBO() {
		return buildXYChartBO;
	}

	/**
	 * @param buildXYChartBO the buildXYChartBO to set
	 */
	public void setBuildXYChartBO(BuildXYChartBO buildXYChartBO) {
		this.buildXYChartBO = buildXYChartBO;
	}

	/**
	 * @return the indexBO
	 */
	public IndexBO getIndexBO() {
		return indexBO;
	}

	/**
	 * @param indexBO the indexBO to set
	 */
	public void setIndexBO(IndexBO indexBO) {
		this.indexBO = indexBO;
	}
	
	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		MainPageCommand mainPageCommand = (MainPageCommand)command;
		if(mainPageCommand.getWhichAction().equals("retornoView")){
			Map<Date, Double> indexCloseReturns = indexBO.getIndexReturnMap("PSI20", 0);
			ChartVO chart = buildXYChartBO.produceCloseIndexReturnChart(indexCloseReturns);
			request.setAttribute("mainPageChart", new XYChartProducer(chart));
			return new ModelAndView("loggedIn", "mainPageCommand", mainPageCommand);
		}else{
			String searchTicker = mainPageCommand.getSearchTicker();
			String market = mainPageCommand.getMarket();
			String type = mainPageCommand.getType();
			List<Stock> stockList = stockBO.getStockList(searchTicker, market, type);
			
			
			request.getSession().setAttribute("stockList", stockList);
			
			return new ModelAndView("assets/assetsMainView", "mainPageCommand", mainPageCommand);
		}
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		loadUserAndUserMenu(request);
		MainPageCommand mainPageCommand = new MainPageCommand();
		mainPageCommand.setMarkets(loadCombosBO.loadMarkets());
		mainPageCommand.setTypes(loadCombosBO.loadTypes());
		//Por defeito carrega as ultimas 52 sessoes do psi20
		List<IndexData> stockList;
		try {
			stockList = indexBO.getIndexDataList("PSI20", 0);
			
			request.getSession().setAttribute("stockList", stockList);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw e;
		}
		ChartVO chart = buildXYChartBO.produceCloseIndexChart(stockList);
		request.setAttribute("mainPageChart", new XYChartProducer(chart));
		return mainPageCommand;
	}
	
	private void loadUserAndUserMenu(HttpServletRequest request){
		//TODO Corrigir o fake login
		UtilizadorVO user = new UtilizadorVO();
		List<String> capacidadesList = new ArrayList<String>();
		capacidadesList.add("00");
		capacidadesList.add("01");
		capacidadesList.add("02");
		capacidadesList.add("03");
		user.setCapacidades(capacidadesList);
		user.setNomeUtilizador("Manuel Leiria");
		user.setUser("mleiria");
		//END fake login
		// Constroi o menu
		MenuDigesterDriver di = new MenuDigesterDriver();
		Menu_Items mi = di.digestMenuFile();
		String jsMenu = di.buildStringMenu(mi, user);
		
		request.getSession().setAttribute(Constants.UserMenu, jsMenu);
		request.getSession().setAttribute(Constants.User, user);
	}
	
}
